﻿using AMMVVMLibrary.Portable;
using Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ViewModels
{
    public class MainPageViewModel : ViewModelBase
    {
        private string _PageName;
        public string PageName
        {
            get { return _PageName; }
            set
            {
                if (value == _PageName)
                {
                    return;
                }
                _PageName = value;
                OnPropertyChanged(() => PageName);
            }
        }

        private ColoredSquare _SelectedItem;
        public ColoredSquare SelectedItem
        {
            get { return _SelectedItem; }
            set
            {
                if (value == _SelectedItem)
                {
                    return;
                }
                _SelectedItem = value;
                OnPropertyChanged(() => SelectedItem);
                MakeGreen.RaiseCanExecuteChanged();
                NavigateToItemPage.RaiseCanExecuteChanged();
            }
        }

        public ObservableCollection<ColoredSquare> SquaresList { get; set; }

        public DelegateCommand MakeGreen { get; set; }

        public DelegateCommand NavigateToItemPage { get; set; }

        public MainPageViewModel()
        {
            PageName = "Squares List";

            SquaresList = new ObservableCollection<ColoredSquare>();

            for (int i = 0; i < 8; i++)
            {
                SquaresList.Add(new ColoredSquare { Id = i, Color = "Orange", Content ="I am orange square number " + i });
            }

            MakeGreen = new DelegateCommand(
                () => { 
                    SelectedItem.Color = "Green";
                    SelectedItem.Content = "I am green square number " + SelectedItem.Id;
                },
                () => { return SelectedItem != null; }
                );

            NavigateToItemPage = new DelegateCommand(
                () => { NavigationService.Navigate<ItemPageViewModel>(SelectedItem); },
                () => { return SelectedItem != null; }
                );
        }
        public override void LoadData(object parameter)
        {
            throw new NotImplementedException();
        }
    }
}
